home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSFS.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  7KB  |  279 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dosfs.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "msdos.h"
  36. #include "osfs.h"
  37.  
  38. int
  39. DEFUN (DOS_read_file_status, (name, s),
  40.        CONST char * name AND
  41.        struct stat * s)
  42. { char filename[128];
  43.  
  44.   dos_pathname_as_filename(name, filename);
  45.   
  46.   while ((stat (filename, s)) < 0)
  47.     {
  48.       if (errno == EINTR)
  49.     continue;
  50.       if ((errno == ENOENT) || (errno == ENOTDIR))
  51.     return (0);
  52.       error_system_call (errno, syscall_lstat);
  53.     }
  54.   return (1);
  55. }
  56.  
  57. enum file_existence
  58. DEFUN (OS_file_existence_test, (name), char * name)
  59. {
  60.   struct stat s;
  61.   char filename[128];
  62.  
  63.   dos_pathname_as_filename(name, filename);
  64.   
  65.   return
  66.     (((DOS_stat (filename, (&s))) < 0)
  67.      ? file_doesnt_exist : file_does_exist);
  68. }
  69.  
  70. int
  71. DEFUN (OS_file_access, (name, mode), CONST char * name AND unsigned int mode)
  72. {
  73.   char filename[128];
  74.  
  75.   dos_pathname_as_filename(name, filename);
  76.   return ((DOS_access (filename, mode)) == 0);
  77. }
  78.  
  79. int
  80. DEFUN (OS_file_directory_p, (name), char * name)
  81. {    
  82.   struct stat s;
  83.   char filename[128];
  84.  
  85.   dos_pathname_as_filename(name, filename);
  86.   return (((DOS_stat (filename, (&s))) == 0) &&
  87.       (((s . st_mode) & S_IFMT) == S_IFDIR));
  88. }
  89.  
  90. CONST char *
  91. DEFUN (OS_file_soft_link_p, (name), CONST char * name)
  92. {
  93.   return (0);
  94. }
  95.  
  96. void
  97. DEFUN (OS_file_remove, (name), CONST char * name)
  98. {
  99.   STD_VOID_SYSTEM_CALL (syscall_unlink, (DOS_unlink (name)));
  100. }
  101.  
  102. void
  103. DEFUN (OS_file_remove_link, (name), CONST char * name)
  104. {
  105.   struct stat s;
  106.   if ( (DOS_stat (name, (&s)) == 0) &&
  107.        (((s . st_mode) & S_IFMT) == S_IFREG) )
  108.    DOS_unlink (name);
  109.   return;
  110. }
  111.  
  112. void
  113. DEFUN (OS_file_link_hard, (from_name, to_name),
  114.        CONST char * from_name AND
  115.        CONST char * to_name)
  116. {
  117.   error_unimplemented_primitive ();
  118. }
  119.  
  120. void
  121. DEFUN (OS_file_link_soft, (from_name, to_name),
  122.        CONST char * from_name AND
  123.        CONST char * to_name)
  124. {
  125.   error_unimplemented_primitive ();
  126. }
  127.  
  128. void
  129. DEFUN (OS_file_rename, (from_name, to_name),
  130.        CONST char * from_name AND
  131.        CONST char * to_name)
  132. {
  133.   if ((rename (from_name, to_name)) != 0)
  134.     error_system_call (errno, syscall_rename);
  135. }
  136.  
  137. void
  138. DEFUN (OS_directory_make, (name), CONST char * name)
  139. {
  140.   STD_VOID_SYSTEM_CALL (syscall_mkdir, (DOS_mkdir (name)));
  141. }
  142.  
  143. /* This is such that directory open does not return the first file */
  144. typedef struct DIR_struct
  145. { struct FIND *entry;
  146.   char pathname[13];
  147. } DIR;
  148.  
  149. #define Get_Directory_Entry_Name(entry, pathname)        \
  150.   (strcpy(pathname, (entry)->name), strlwr(pathname))
  151.  
  152. int OS_directory_index;
  153.  
  154. static DIR ** directory_pointers;
  155. static unsigned int n_directory_pointers;
  156.  
  157. void
  158. DEFUN_VOID (DOS_initialize_directory_reader)
  159. {
  160.   directory_pointers = 0;
  161.   n_directory_pointers = 0;
  162.   OS_directory_index = (-1);
  163. }
  164.  
  165. static unsigned int
  166. DEFUN (allocate_directory_pointer, (pointer), DIR * pointer)
  167. {
  168.   if (n_directory_pointers == 0)
  169.     {
  170.       DIR ** pointers = ((DIR **) (DOS_malloc ((sizeof (DIR *)) * 4)));
  171.       if (pointers == 0)
  172.     error_system_call (ENOMEM, syscall_malloc);
  173.       directory_pointers = pointers;
  174.       n_directory_pointers = 4;
  175.       {
  176.     DIR ** scan = directory_pointers;
  177.     DIR ** end = (scan + n_directory_pointers);
  178.     (*scan++) = pointer;
  179.     while (scan < end)
  180.       (*scan++) = 0;
  181.       }
  182.       return (0);
  183.     }
  184.   {
  185.     DIR ** scan = directory_pointers;
  186.     DIR ** end = (scan + n_directory_pointers);
  187.     while (scan < end)
  188.       if ((*scan++) == 0)
  189.     {
  190.       (*--scan) = pointer;
  191.       return (scan - directory_pointers);
  192.     }
  193.   }
  194.   {
  195.     unsigned int result = n_directory_pointers;
  196.     unsigned int n_pointers = (2 * n_directory_pointers);
  197.     DIR ** pointers =
  198.       ((DIR **)
  199.        (DOS_realloc (((PTR) directory_pointers),
  200.             ((sizeof (DIR *)) * n_pointers))));
  201.     if (pointers == 0)
  202.       error_system_call (ENOMEM, syscall_realloc);
  203.     {
  204.       DIR ** scan = (pointers + result);
  205.       DIR ** end = (pointers + n_pointers);
  206.       (*scan++) = pointer;
  207.       while (scan < end)
  208.     (*scan++) = 0;
  209.     }
  210.     directory_pointers = pointers;
  211.     n_directory_pointers = n_pointers;
  212.     return (result);
  213.   }
  214. }
  215.  
  216. #define REFERENCE_DIRECTORY(index) (directory_pointers[(index)])
  217. #define DEALLOCATE_DIRECTORY(index) ((directory_pointers[(index)]) = 0)
  218.  
  219. int
  220. DEFUN (OS_directory_valid_p, (index), long index)
  221. {
  222.   return
  223.     ((0 <= index)
  224.      && (index < n_directory_pointers)
  225.      && ((REFERENCE_DIRECTORY (index)) != 0));
  226. }
  227.  
  228. unsigned int
  229. DEFUN (OS_directory_open, (name), CONST char * name)
  230.   char filename[128], searchname[128];
  231.   struct FIND *entry;
  232.   DIR * pointer = malloc(sizeof(DIR));
  233.   
  234.   if (pointer == 0)
  235.     error_system_call (ENOMEM, syscall_malloc);
  236.  
  237.   if (dos_pathname_as_filename(name, filename))
  238.     sprintf(searchname, "%s*.*", filename);
  239.   else
  240.     sprintf(searchname, "%s\\*.*", filename);
  241.  
  242.   if ((entry = findfirst(searchname, FA_DIREC)) == 0)
  243.     error_system_call (errno, syscall_opendir);
  244.   
  245.   pointer->entry = entry;
  246.   return (allocate_directory_pointer (pointer));
  247. }
  248.  
  249. CONST char *
  250. DEFUN (OS_directory_read, (index), unsigned int index)
  251. { DIR * pointer = REFERENCE_DIRECTORY (index);
  252.   if (pointer->entry == 0)
  253.     return 0;
  254.  
  255.   Get_Directory_Entry_Name(pointer->entry, pointer->pathname);
  256.   pointer->entry = findnext();
  257.   return (pointer -> pathname);
  258. }
  259.  
  260. CONST char *
  261. DEFUN (OS_directory_read_matching, (index, prefix),
  262.        unsigned int index AND
  263.        CONST char * prefix)
  264. {
  265.   error_unimplemented_primitive ();
  266.   return (0);
  267. }
  268.  
  269. void
  270. DEFUN (OS_directory_close, (index), unsigned int index)
  271. { DIR * pointer = REFERENCE_DIRECTORY (index);
  272.  
  273.   free(pointer);
  274.   DEALLOCATE_DIRECTORY (index);
  275. }
  276.  
  277.  
  278.